package kosta.aop;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import kosta.model.board.BoardVO;
import kosta.model.member.MemberDao;
import kosta.model.member.MemberVO;
import kosta.model.reply.ReplyVO;
import kosta.model.reply.dis.DisReplyVO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class PointAspect {
	@Resource(name = "memberDaoImpl")
	private MemberDao memberDao;
	private Log log = LogFactory.getLog(getClass());
	private HttpSession session;
	/**
	 * 게시판 글쓰기 포인트 추가
	 * @param point
	 * @throws Throwable
	 */
	@After("execution(* kosta.model.board.*Service.insert*(..))")
	public void plusPointBoard(JoinPoint point) throws Throwable {
		Object params[] = point.getArgs();
		/*
		 * String cn = point.getTarget().getClass().getName();
		 * String mn =point.getSignature().getName();
		 * for(int i=0; i<params.length; i++) {
		 * log.info(cn+" "+mn+ "test "+i+"번째 "+params[i]); }
		 */
		BoardVO bvo = (BoardVO) params[0];
		memberDao.plusPoint(bvo.getMemberVO().getId());
		MemberVO mvo = memberDao.idCheck(bvo.getMemberVO().getId());
		//System.out.println(mvo + " aop");
		//MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		//session.invalidate();
		//session.setAttribute("mvo", mvo);
	}
	
	/**
	 * 게시판 댓글쓰기 포인트 추가
	 * @param point
	 * @throws Throwable
	 */
	@After("execution(* kosta.model.reply.*Service.insert*(..))")
	public void plusPointReply(JoinPoint point) throws Throwable {
		Object params[] = point.getArgs();
		ReplyVO rvo = (ReplyVO) params[0];
		memberDao.plusPoint(rvo.getMemberVO().getId());
		MemberVO mvo = memberDao.idCheck(rvo.getMemberVO().getId());
	}
	
	/**
	 * 토론게시판 댓글쓰기 포인트 추가
	 * @param point
	 * @throws Throwable
	 */
	@After("execution(* kosta.model.reply.dis.*Service.insert*(..))")
	public void plusPointDisReply(JoinPoint point) throws Throwable {
		Object params[] = point.getArgs();
		DisReplyVO rvo = (DisReplyVO) params[0];
		memberDao.plusPoint(rvo.getMemberVO().getId());
		MemberVO mvo = memberDao.idCheck(rvo.getMemberVO().getId());
	}
	
	/**
	 * 삭제시 포인트 차감
	 * @param point
	 * @throws Throwable
	 */
	@After("execution(* kosta.model..*Service.delete*(..))")
	public void minusPoint(JoinPoint point) throws Throwable {
		Object params[] = point.getArgs();
		String id = (String) params[1];
		memberDao.minusPoint(id);
	}
}
